<?php
require_once($server->my_site[0]->path.$server->my_site[0]->folder."distributor_manage.php");
//we ensure it is an allowed user
$distributor->id or exit("Área reservada a distribuidores");

if (isset($_POST['searchterm'])) {
	//We first create a sql condition term from the search data
	$search_terms=explode(';',$_POST['searchterm']);
	foreach ($search_terms as $search_term) {
		$query = $distributor->searchTermToQuery($search_term);
	}
	// $query is the sql condition. From it we create the database search
	$distributor->processed_orders=$distributor->get_orders_search($query, 50);
	//The resulting orders are not filled with data, they just have the order id. Next lines are for filling them
	for ($i=0; $i < count($distributor->processed_orders); $i++) {
		$distributor->processed_orders[$i]->db_load();
		$distributor->processed_orders[$i]->db_load_items();
		$cartPrice=0;
		foreach ($distributor->processed_orders[$i]->items as $item) {
			$cartPrice+=$item->price * $item->quantity;
		}
		$distributor->processed_orders[$i]->price=$cartPrice;
		$distributor->processed_orders[$i]->payment=new payment();
		$distributor->processed_orders[$i]->payment->method=new payment_method();
		$distributor->processed_orders[$i]->payment->method->id=$distributor->processed_orders[$i]->payment_method_id;
		$distributor->processed_orders[$i]->payment->method->db_load();
		$distributor->processed_orders[$i]->delivery=new delivery();
		$distributor->processed_orders[$i]->delivery->phone=$distributor->processed_orders[$i]->phone;
		if ($distributor->processed_orders[$i]->sec_phone) $distributor->processed_orders[$i]->delivery->sec_phone=$distributor->processed_orders[$i]->sec_phone;
		$distributor->processed_orders[$i]->delivery->shipping_type=new shipping_type();
		$distributor->processed_orders[$i]->delivery->shipping_type->id=$distributor->processed_orders[$i]->shipping_type_id;
		$distributor->processed_orders[$i]->delivery->shipping_type->db_load();
		$distributor->processed_orders[$i]->delivery->address=new address();
		if ($distributor->processed_orders[$i]->address_id) {
			$distributor->processed_orders[$i]->delivery->address->id=$distributor->processed_orders[$i]->address_id;
		}
		else {
			$distributor->processed_orders[$i]->delivery->address->id=$user->address_id;
		}
		$distributor->processed_orders[$i]->delivery->address->db_load();
		$distributor->processed_orders[$i]->delivery->address->country=new country();
		$distributor->processed_orders[$i]->delivery->address->country->id=$distributor->processed_orders[$i]->delivery->address->country_id;
		$distributor->processed_orders[$i]->delivery->address->country->db_load();
		$distributor->processed_orders[$i]->delivery->address->zone=new zone();
		$distributor->processed_orders[$i]->delivery->address->zone->id=$distributor->processed_orders[$i]->delivery->address->zone_id;
		$distributor->processed_orders[$i]->delivery->address->zone->db_load();
	}
}
else $distributor->processed_orders=array();
// The next part is to take unprocessed orders, equivalent to the above part but search is just in the status condition.
$distributor->unprocessed_orders=$distributor->get_orders('=1', 100, 'status_date ASC');
for ($i=0; $i < count($distributor->unprocessed_orders); $i++) {
	$distributor->unprocessed_orders[$i]->db_load();
	$distributor->unprocessed_orders[$i]->db_load_items();
	$cartPrice=0;
	foreach ($distributor->unprocessed_orders[$i]->items as $item) {
		$cartPrice+=$item->price * $item->quantity;
	}
	$distributor->unprocessed_orders[$i]->subTotal=$cartPrice;
	$distributor->unprocessed_orders[$i]->payment=new payment();
	$distributor->unprocessed_orders[$i]->payment->method=new payment_method();
	$distributor->unprocessed_orders[$i]->payment->method->id=$distributor->unprocessed_orders[$i]->payment_method_id;
	$distributor->unprocessed_orders[$i]->payment->method->db_load();
	$distributor->unprocessed_orders[$i]->delivery=new delivery();
	$distributor->unprocessed_orders[$i]->delivery->phone=$distributor->unprocessed_orders[$i]->phone;
	if ($distributor->unprocessed_orders[$i]->sec_phone) $distributor->unprocessed_orders[$i]->delivery->sec_phone=$distributor->unprocessed_orders[$i]->sec_phone;
	$distributor->unprocessed_orders[$i]->delivery->shipping_type=new shipping_type();
	$distributor->unprocessed_orders[$i]->delivery->shipping_type->id=$distributor->unprocessed_orders[$i]->shipping_type_id;
	$distributor->unprocessed_orders[$i]->delivery->shipping_type->db_load();
	$distributor->unprocessed_orders[$i]->delivery->address=new address();
	if ($distributor->unprocessed_orders[$i]->address_id) {
		$distributor->unprocessed_orders[$i]->delivery->address->id=$distributor->unprocessed_orders[$i]->address_id;
	}
	else {
		$distributor->unprocessed_orders[$i]->delivery->address->id=$user->address_id;
	}
	$distributor->unprocessed_orders[$i]->delivery->address->db_load();
	$distributor->unprocessed_orders[$i]->delivery->address->country=new country();
	$distributor->unprocessed_orders[$i]->delivery->address->country->id=$distributor->unprocessed_orders[$i]->delivery->address->country_id;
	$distributor->unprocessed_orders[$i]->delivery->address->country->db_load();
	$distributor->unprocessed_orders[$i]->delivery->address->zone=new zone();
	$distributor->unprocessed_orders[$i]->delivery->address->zone->id=$distributor->unprocessed_orders[$i]->delivery->address->zone_id;
	$distributor->unprocessed_orders[$i]->delivery->address->zone->db_load();
}

$_SESSION["ser_distributor"]=serialize($distributor);
?>
	<table style="width:100%;">
		<tr>
			<td class="orders">
<?php
$gmtime=mktime(gmdate("H"), gmdate("i"), gmdate("s"), gmdate("n")  , gmdate("j"), gmdate("Y"));

foreach ($distributor->unprocessed_orders as $order) {
	$order_class="previous";	// Orders that have been made before today are showed in red.
	if (date('y-m-d')== date('y-m-d', $gmtime) ) $order_class="today";
?>
				<table	class="button <?php print $order_class; ?>"><tr><td><a href="?section=order&order_id=<?php print $order->id; ?>" myFor="#results"><?php print $order->delivery->address->firstname; ?></a></td></tr></table>
<?php
}
foreach ($distributor->processed_orders as $order) {
?>
				<table	class="button processed"><tr><td><a href="?section=order&order_id=<?php print $order->id; ?>" myFor="#results"><?php print $order->delivery->address->firstname; ?></a></td></tr></table>
<?php
}
?>
			</td>
			<td class="menu">
				<div style="">
					<table class="button"><tr><td><a href="#" id="allButton">Seleccionar todo</a></td></tr></table>
					<table class="button"><tr><td><a href="?section=order_print&labels=1" id="printButton" myFor="#results">Imprimir</a></td></tr></table>
					<table class="button"><tr><td><a href="?section=order_process" id="processButton" myFor="#results">Procesar</a></td></tr></table>
				</div>
			</td>
		</tr>
	</table>

<script language="JavaScript">
 setCheckButtons();
 setNewordersButtons();
</script>
